package com.hxh.learn.controller;

import com.hxh.common.unification.ResultOutcome;
import com.hxh.learn.model.AdminLogin;
import com.hxh.learn.model.SysUser;
import com.hxh.learn.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

import static com.hxh.common.constant.HxhConstant.LOGIN_SUCCESS;

/**
 * @Author: hxh
 * @Date: 2023/3/6
 */
@Controller
@RequestMapping("/user")
@RestController
@Api(tags = "用户登录")
public class AdminLoginController {
    @Autowired
    private ISysUserService sysUserService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ResultOutcome<String> add(@Validated @RequestBody AdminLogin adminLogin) {
        String result = sysUserService.login(adminLogin.getUsername(), adminLogin.getPassword());
        return ResultOutcome.successResult(LOGIN_SUCCESS,result);
    }
    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    public ResultOutcome<SysUser> add(Principal principal) {
        if (null==principal){
            throw new RuntimeException("当前用户信息不存在");
        }
        String name = principal.getName();
        SysUser sysUser = sysUserService.getByName(name);
        //密码不需要展示给用户
        return ResultOutcome.successResult(sysUser);
    }

}
